package com.ruoyi.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ruoyi.system.domain.BsRelation;
import com.ruoyi.system.domain.dto.BsRelationNumDTO;
import com.ruoyi.system.domain.dto.YoukeCenterInfoDTO;
import com.ruoyi.system.domain.vo.BsRelationVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 关系记录（关注、收藏、点赞）Mapper接口
 *
 * @author ruoyi
 * @date 2023-11-25
 */
public interface BsRelationMapper extends BaseMapper<BsRelation> {


    //注意$不要改
    @Select("select aim_id aimId, count(aim_id) countNum \n" +
            "from bs_relation \n" +
            "where  aim_id in (${idsString}) and del_flag = '0' and type=#{type} \n" +
            "group by aim_id ")
    List<BsRelationNumDTO> getTotalRelationByType(@Param("idsString") String idsString, @Param("type") String type);

    //注意$不要改
    @Select("select aim_id aimId \n" +
            "from bs_relation \n" +
            "where  aim_id in (${idsString}) and create_by_id = #{userId} and del_flag = '0' and type=#{type} \n" +
            "group by aim_id ")
    List<String> getRelationByType(@Param("userId") Long userId, @Param("idsString") String idsString, @Param("type") String type);


    /**
     * 说明 : 关注列表
     */
    @Select("select main.*, if(temp.id is null, false, true) attentioned\n" +
            "from bs_relation main\n" +
            "         left join bs_relation temp on (main.create_by_id = temp.aim_id and main.aim_id = temp.create_by_id and temp.del_flag='0' and temp.type=#{type})\n" +
            "where main.create_by_id = #{userId}\n" +
            "  and main.del_flag = '0'\n" +
            "  and main.type = #{type} ")
    List<BsRelationVO> selectListByAttention(@Param("userId") Long userId, @Param("type") String type);

    /**
     *  说明 : 粉丝
     */
    @Select("select main.*, if(temp.id is null, false, true) attentioned\n" +
            "from bs_relation main\n" +
            "         left join bs_relation temp on (main.create_by_id = temp.aim_id and main.aim_id = temp.create_by_id and temp.del_flag='0' and temp.type=#{type})\n" +
            "where main.aim_id = #{userId}\n" +
            "  and main.del_flag = '0'\n" +
            "  and main.type = #{type} ")
    List<BsRelationVO> selectListByAttentioned(@Param("userId") Long userId, @Param("type") String type);


    @Select("select max(attentionNum) attentionNum, max(attentionedNum) attentionedNum, max(strategyNum) strategyNum, max(commentNum) commentNum from\n" +
            "(select count(main.id) attentionNum, 0 attentionedNum, 0 strategyNum, 0 commentNum\n" +
            "from bs_relation main\n" +
            "where main.create_by_id = #{userId}\n" +
            "  and main.del_flag = '0'\n" +
            "  and main.type = 'customer_attention'\n" +
            "union all\n" +
            "select 0 attentionNum, count(main.id) attentionedNum, 0 strategyNum, 0 commentNum\n" +
            "from bs_relation main\n" +
            "where main.aim_id = #{userId}\n" +
            "  and main.del_flag = '0'\n" +
            "  and main.type = 'customer_attention'\n" +
            "union all\n" +
            "select 0 attentionNum, 0 attentionedNum, count(main.id) strategyNum, 0 commentNum\n" +
            "from bs_strategy main\n" +
            "where main.create_by_id = #{userId}\n" +
            "  and main.del_flag = '0'\n" +
            "union all\n" +
            "select 0 attentionNum, 0 attentionedNum, 0 strategyNum, count(main.id) commentNum\n" +
            "from bs_strategy main\n" +
            "left join bs_comment temp on main.id=temp.aim_id\n" +
            "where main.create_by_id = #{userId}\n" +
            "  and main.del_flag = '0') temp")
    YoukeCenterInfoDTO youkeCenterCount(@Param("userId") Long userId);
}
